package com.basic.auth.controller;

import com.basic.auth.controller.req.AuthFuncReqForm;
import com.basic.auth.controller.res.AuthFuncResForm;
import com.basic.auth.controller.res.AuthUserFuncResForm;
import com.basic.auth.dao.model.AuthFunc;
import com.basic.auth.domain.AuthFuncBo;
import com.basic.auth.domain.AuthUserFuncBo;
import com.basic.auth.service.AuthFuncService;
import com.basic.common.bean.BeanCopierDsUtil;
import com.basic.common.bean.BeanCopyDsUtil;
import com.basic.common.result.Errors;
import com.basic.common.result.WebResult;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能管理controller
 *
 * @author 
 * @version 1.0
 * @Date 
 */
@RestController
@RequestMapping(value = "/authFunc")

public class AuthFuncController {

    @Autowired
    private AuthFuncService authFuncService;


    /**
     * 递归查询
     *
     * @param funcReqForm
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @date 15:18 
     */

    @RequestMapping(value = "/selSubList", method = RequestMethod.POST)
    public WebResult selSubList( @RequestBody AuthFuncReqForm funcReqForm) {

        AuthFuncBo authFuncBo = new AuthFuncBo();
        BeanCopyDsUtil.copyProperties(funcReqForm, authFuncBo);
        if (authFuncBo.getParentFuncId() == null) {
            authFuncBo.setParentFuncId(0L);
        }
        List<AuthFuncBo> authFuncList = authFuncService.selSubList(authFuncBo);
        List<AuthFuncResForm> resFormList = modelBoToResList(authFuncList);
        return WebResult.success(resFormList);
    }

    /**
     * 递归查询子孙功能集合（不包含自身） 包含是否可用
     *
     * @param funcReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @date 11:21
     */
//    @ApiOperation(value = "递归查询子孙功能集合(不包含自身)，包含是否可用", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selSubFuncList", method = RequestMethod.POST)
    public WebResult selSubFuncList( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(null, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = new AuthFuncBo();
        BeanCopyDsUtil.copyProperties(funcReqForm, authFuncBo);
        if (authFuncBo.getParentFuncId() == null) {
            authFuncBo.setParentFuncId(0L);
        }
        List<AuthFuncBo> authFuncList = authFuncService.selSubFuncList(authFuncBo);
        List<AuthFuncResForm> resFormList = modelBoToResList(authFuncList);
        return WebResult.success(resFormList);
    }

    /**
     * 根据条件查询功能集合(普通查询)
     *
     * @param funcReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @since  13:27
     */
//    @ApiOperation(value = "根据条件查询功能集合", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selFuncListByParam", method = RequestMethod.POST)
    public WebResult selFuncListByParam( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = new AuthFuncBo();
        BeanCopyDsUtil.copyProperties(funcReqForm, authFuncBo);
        if (authFuncBo.getParentFuncId() == null) {
            authFuncBo.setParentFuncId(0L);
        }
        List<AuthFunc> authFuncList = authFuncService.selFuncListByParam(authFuncBo);
        List<AuthFuncResForm> authFuncResFormList = BeanCopierDsUtil.copyPropertiesOfList(authFuncList, AuthFuncResForm.class);
        return WebResult.success(authFuncResFormList);
    }

    /**
     * 根据条件查询功能集合(不用传parentFuncId)
     *
     * @param funcReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @since  13:27
     */
//    @ApiOperation(value = "根据条件查询功能集合", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selFuncByParam", method = RequestMethod.POST)
    public WebResult selListByParam(@RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = new AuthFuncBo();
        BeanCopyDsUtil.copyProperties(funcReqForm, authFuncBo);
        List<AuthFunc> authFuncList = authFuncService.selFuncListByParam(authFuncBo);
        List<AuthFuncResForm> authFuncResFormList = BeanCopierDsUtil.copyPropertiesOfList(authFuncList, AuthFuncResForm.class);
        return WebResult.success(authFuncResFormList);
    }

    /**
     * 根据条件递归查询子孙功能集合，只有可用的
     *
     * @param funcReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @since  13:26
     */
//    @ApiOperation(value = "根据条件递归查询子孙功能集合（可用）", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selSubFuncListByParam", method = RequestMethod.POST)
    public WebResult selSubFuncListByParam( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = new AuthFuncBo();
        BeanCopyDsUtil.copyProperties(funcReqForm, authFuncBo);
        List<AuthFuncBo> authFuncList = authFuncService.selSubFuncListByParam(authFuncBo);
        List<AuthFuncResForm> authFuncResFormList = modelBoToResList(authFuncList);
        return WebResult.success(authFuncResFormList);
    }

    /**
     * 查询用户功能权限集合
     *
     * @param funcReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @since  13:26
     */
//    @ApiOperation(value = "查询用户功能权限", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selAuthFuncByParam", method = RequestMethod.POST)
    public WebResult selAuthFuncByParam( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = new AuthFuncBo();
        BeanCopyDsUtil.copyProperties(funcReqForm, authFuncBo);
        List<AuthUserFuncBo> authUserFuncBoList = authFuncService.selAuthFuncByParam(authFuncBo);
        List<AuthUserFuncResForm> authUserFuncResFormList = BeanCopierDsUtil.copyPropertiesOfList(authUserFuncBoList, AuthUserFuncResForm.class);
        return WebResult.success(authUserFuncResFormList);
    }

//    @ApiOperation(value = "根据功能id查询功能数据", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selAuthFuncById", method = RequestMethod.POST)
    public WebResult selAuthFuncById( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFunc authFunc = authFuncService.selAuthFuncById(funcReqForm.getId());
        AuthFuncResForm authFuncResForm = modelToRes(authFunc);
        return WebResult.success(authFuncResForm);
    }

//    @ApiOperation(value = "根据功能id修改功能数据", notes = "修改参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "修改参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/updAuthFuncById", method = RequestMethod.POST)
    public WebResult updAuthFuncById( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFunc authFunc = BeanCopierDsUtil.copyProperties(funcReqForm, AuthFunc.class);
        authFuncService.updAuthFuncById(authFunc);
        return WebResult.success();
    }


//    @ApiOperation(value = "根据功能id删除功能模块", notes = "删除参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "删除参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/delAuthFuncById", method = RequestMethod.POST)
    public WebResult delAuthFuncById( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        authFuncService.delAuthFuncById(funcReqForm.getId());
        return WebResult.success();
    }

//    @ApiOperation(value = "新增功能模块", notes = "添加参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "添加参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/addAuthFunc", method = RequestMethod.POST)
    public WebResult addAuthFunc( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {0
//            return webAuthResult.getWebResult();
//        }
//        AuthFunc authFunc = new AuthFunc();
//        BeanCopyDsUtil.copyProperties(funcReqForm, authFunc);
        AuthFunc authFunc = BeanCopierDsUtil.copyProperties(funcReqForm, AuthFunc.class);

        try {
            authFuncService.addAuthFunc(authFunc);
            return WebResult.success();
        }catch (Exception e){

            return WebResult.fail(Errors.SYSTEM_INSERT_ERROR);
        }

    }

    /**
     * 分页查询
     *
     * @param funcReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @since  10:04
     */
//    @ApiOperation(value = "分页查询菜单列表", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "查询参数", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/selSubFuncPageList", method = RequestMethod.POST)
    public WebResult selSubFuncPageList( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(null, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = BeanCopierDsUtil.copyProperties(funcReqForm, AuthFuncBo.class);
        WebResult webResult = authFuncService.selSubFuncPageList(authFuncBo);
        PageInfo pageInfo = (PageInfo) webResult.getData();
        List<AuthFuncBo> authFuncBoList = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(authFuncBoList)) {
            List<AuthFuncResForm> resFormList = modelBoToResList(authFuncBoList);
            pageInfo.setList(resFormList);
        }
        return WebResult.success(pageInfo);
    }

    /**
     * 根据code查询用户是否有该模块权限
     *
     * @param funcReqForm
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @date 15:15
     */
//    @ApiOperation(value = "判断用户是否有该模块权限", notes = "判断用户是否有该模块权限")
//    @ApiImplicitParam(name = "AuthFuncReqForm ", value = "判断用户是否有该模块权限", required = true, dataType = "AuthFuncReqForm ")
    @RequestMapping(value = "/judgeUserFuncByCode", method = RequestMethod.POST)
    public WebResult judgeUserFuncByCode( @RequestBody AuthFuncReqForm funcReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(funcReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthFuncBo authFuncBo = AuthFuncBo.reqToBo(funcReqForm);
        Boolean b = authFuncService.judgeUserFuncByCode(authFuncBo);
        return WebResult.success(b);
    }

    private List<AuthFuncResForm> modelBoToResList(List<AuthFuncBo> authFuncBoList) {
        if (CollectionUtils.isEmpty(authFuncBoList)) {
            return null;
        }
        List<AuthFuncResForm> result = new ArrayList<AuthFuncResForm>();
        AuthFuncResForm resForm = null;
        for (AuthFuncBo authFuncBo : authFuncBoList) {
            resForm = modelBoToRes(authFuncBo);
            result.add(resForm);
        }
        return result;
    }

    private AuthFuncResForm modelBoToRes(AuthFuncBo authFuncBo) {
        if (authFuncBo == null) {
            return null;
        }
        AuthFuncResForm resForm = BeanCopierDsUtil.copyProperties(authFuncBo, AuthFuncResForm.class);
        resForm.setSubFuncList(modelBoToResList(authFuncBo.getSubFuncList()));
        return resForm;
    }

    private AuthFuncResForm modelToRes(AuthFunc authFunc) {
        if (authFunc == null) {
            return null;
        }
        AuthFuncResForm resForm = BeanCopierDsUtil.copyProperties(authFunc, AuthFuncResForm.class);
        //封装父级名称
        Long parentFuncId = authFunc.getParentFuncId();
        if (parentFuncId != 0L) {
            AuthFunc authFunc1 = authFuncService.selAuthFuncById(parentFuncId);
            if (authFunc1 != null) {
                resForm.setParentName(authFunc1.getName());
            }
        }
        return resForm;
    }

    @RequestMapping(value = "/selectAuthAll",method = RequestMethod.POST )
    private WebResult selectAuthAll(@RequestBody AuthFuncBo authFuncBo){
        return WebResult.success(authFuncService.selectAuthAll(authFuncBo));

    }

    @RequestMapping(value = "/selectAuthAllByPage", method = {RequestMethod.GET, RequestMethod.POST})
    public WebResult selectAuthAllByPage(@RequestBody AuthFuncBo bo) {

        return WebResult.success(authFuncService.selectAuthAllByPage(bo));
    }

}
